//
//  ViewController.m
//  app
//
//  Created by mac on 2021/6/29.
//
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)

#import "ViewController.h"

//#import "RCDraggableButton.h"

//#import "YZDraggeMoveView.h"

//#import "YZClearUIView.h"

#import "Masonry.h"

//#import "SDWebImage.h"

#import "Toast.h"

#import "ArtScrollView.h"

#import "MJRefresh.h"



@interface ViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,assign)CGFloat redHeight;
@property(nonatomic,assign)CGFloat blueHeight;
@property(nonatomic,strong)UIView * redView;
@property(nonatomic,strong)UIView * blueView;
@property(nonatomic,strong)ArtScrollView * scrollView;
@property(nonatomic,strong)UIScrollView * scrollInnerView;
@property(nonatomic,strong)NSMutableArray * array;
@property(nonatomic,strong)UITableView * tableView;

@property (nonatomic, assign) BOOL vccanScroll;   // 这里的布尔值类似一个锁,初始化的默认值是YES,当用户拖拽了tableview背后的scrollview并且拖拽到了scrollview的偏移距离大于blueview的时候vccanScroll值为NO,锁住了scrollview,不让scrollview进行偏移,不管往上滑动还是往下滑动,并将scrollview的偏移量改为blueview.height.当且仅当tableView.offset.y < 0的时候,也就是tableView被进行了下拉操作的时候,这种情况下说明tableview已经进入到了最顶端的位置,这时候,可以对scrollview进行滑动解锁,也就是把vccanScroll的值再改为YES,这种情况下可以将scrollview可以正常上下滑动了

@end



@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _vccanScroll = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    self.redHeight = 180;
    self.blueHeight = 200;
    self.array = [NSMutableArray arrayWithCapacity:0];
    
    for (int i = 0 ; i < 30; i ++) {
        [self.array addObject:[NSString stringWithFormat:@"need + %d",i]];
    }
    
    [self.view addSubview:self.redView];
    
    [self.view addSubview:self.scrollView];
    self.scrollView.backgroundColor = [UIColor purpleColor];

}


-(UIView *)redView {
    if (_redView == nil) {
        _redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH  , _redHeight)];
        _redView.backgroundColor = [UIColor redColor];
    }
    return _redView;
}

-(UIView *)blueView {
    if (_blueView == nil) {
        _blueView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _blueHeight)];
        _blueView.backgroundColor = [UIColor blueColor];
        UIButton * button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.backgroundColor = [UIColor whiteColor];
        [button setTitle:@"change blueHeight" forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
        [button addTarget:self action:@selector(changeBlueValue) forControlEvents:(UIControlEventTouchUpInside)];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_blueView.mas_centerX);
            make.centerY.equalTo(_blueView.mas_centerY);
            make.width.equalTo(@200);
            make.height.equalTo(@50);
        }];
        [_blueView addSubview:button];
    }
    return _blueView;
}

-(void)changeBlueValue {

    if (self.blueHeight == 240) {
        self.blueHeight = 200;
    }else
    {
        self.blueHeight = 240;
    }
    
    [self changeBindingFrame];
}

-(void)changeBindingFrame {
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, (SCREEN_HEIGHT - _redHeight) + _blueHeight);
    _scrollInnerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, (SCREEN_HEIGHT - _redHeight) + _blueHeight);
    _scrollInnerView.contentSize = CGSizeMake(SCREEN_WIDTH, (SCREEN_HEIGHT - _redHeight) + _blueHeight);
    self.blueView.frame = CGRectMake(0, 0, SCREEN_WIDTH, _blueHeight);
    self.tableView.frame = CGRectMake(0, _blueHeight, SCREEN_WIDTH, SCREEN_HEIGHT - _redHeight);
}


-(ArtScrollView *)scrollView {
    if(_scrollView == nil){
        _scrollView = [[ArtScrollView alloc] initWithFrame:CGRectMake(0, _redHeight, SCREEN_WIDTH, (SCREEN_HEIGHT - _redHeight))];
        _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, (SCREEN_HEIGHT - _redHeight) + _blueHeight);
        _scrollView.backgroundColor = [UIColor grayColor];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.delegate = self;
        _scrollView.bounces = NO;
        
        [_scrollView addSubview:self.scrollInnerView];
        
    }
    return _scrollView;
}

-(UIScrollView *)scrollInnerView {
    if (_scrollInnerView == nil) {
        _scrollInnerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (SCREEN_HEIGHT - _redHeight) + _blueHeight)];
        _scrollInnerView.contentSize = CGSizeMake(SCREEN_WIDTH, (SCREEN_HEIGHT - _redHeight) + _blueHeight);
        _scrollInnerView.showsVerticalScrollIndicator = NO;
        _scrollInnerView.delegate = self;
        _scrollInnerView.backgroundColor = [UIColor yellowColor];
        
        [_scrollInnerView addSubview:self.blueView];
        [_scrollInnerView addSubview:self.tableView];
    }
    return _scrollInnerView;
}

#pragma mark --------- tableView

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, _blueHeight, SCREEN_WIDTH, SCREEN_HEIGHT - _redHeight) style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 55;
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
             [self loadDataMore];
         }];
        _tableView.showsVerticalScrollIndicator = NO;
    }
    
    return _tableView;
}

-(void)loadData {
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
    [self.view makeToast:@"下拉刷新了一次" duration:1 position:CSToastPositionCenter style:[[CSToastStyle alloc] initWithDefaultStyle]];

}

-(void)loadDataMore {
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
    [self.view makeToast:@"上拉加载了一次" duration:1 position:CSToastPositionCenter style:[[CSToastStyle alloc] initWithDefaultStyle]];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [[UITableViewCell alloc] init];
    cell.textLabel.text = self.array[indexPath.row];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat offsetY = scrollView.contentOffset.y;

    if (scrollView == self.scrollView) {
        
        CGFloat maxOffsetY = _blueHeight;
       if (offsetY >= maxOffsetY) {
            scrollView.contentOffset = CGPointMake(0, maxOffsetY);
            _vccanScroll = NO;
        }else {
            if (_vccanScroll == NO) {
                scrollView.contentOffset = CGPointMake(0, maxOffsetY);
            }
        }
    }else if(scrollView == self.tableView){
        CGPoint point = [scrollView.panGestureRecognizer translationInView:scrollView];
        CGFloat taboffsetY = point.y;
        if (offsetY < 0) {
            _vccanScroll = YES;
        }
           if (taboffsetY < 0) {
               if(self.scrollView.contentOffset.y < _blueHeight){
                   self.tableView.contentOffset = CGPointZero;
               }
           } else {
               if (offsetY > 0) {
                   self.scrollView.contentOffset = CGPointMake(0, _blueHeight);
               }else if (offsetY < 0){
                   if (self.scrollView.contentOffset.y > 0 && self.scrollView.contentOffset.y < _blueHeight) {
                       self.tableView.contentOffset = CGPointZero;
                   }
               }
              
           }
        
    }
}





@end

